﻿using BusinessEntity;
using BusinessEntity.Schedule;
using BusinessRepository;
using learun.util;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule
{
    public class ScheduleRule
    {

        #region 获取数据

        /// <summary> 
        /// 获取页面显示列表数据 
        /// </summary> 
        /// <param name="pagination">分页参数</param>
        /// <param name="queryJson">查询参数</param> 
        /// <returns></returns> 
        public Task<IEnumerable<TSSchemeInfoEntity>> GetPageList(Pagination pagination, string queryJson)
        {
            var strSql = new StringBuilder();
            strSql.Append(@" 
                    SELECT
	                    t.F_Id,
	                    t.F_Name,
	                    t.F_Description,
                        t.F_IsActive,
	                    t.F_BeginTime,
                        t.F_EndType,
	                    t.F_EndTime
                    FROM
	                    TS_SchemeInfo t
                ");
            strSql.Append("  WHERE 1=1 ");
            var queryParam = queryJson.ToJObject();
            string keyWord = "";
            if (!queryParam["keyWord"].IsEmpty())
            {
                keyWord = "%" + queryParam["keyWord"].ToString() + "%";
                strSql.Append("  AND t.F_Name like @keyWord ");
            }

            return new RepositoryFactory().BaseRepository().FindList<TSSchemeInfoEntity>(strSql.ToString(), new { keyWord }, pagination);
        }

        //获取
        public static Task<IEnumerable<TSSchemeInfoEntity>> GettSSchemeList()
        {
            var strSql = new StringBuilder();
            strSql.Append(@" 
                    SELECT
	                    t.F_Id,
	                    t.F_Name,
                        t.F_IsActive,
	                    t.F_BeginTime,
                        t.F_EndType,
	                    t.F_EndTime,
                        t.F_Description,
                        t.F_Scheme
                    FROM
	                    TS_SchemeInfo t
                ");
            strSql.Append("  WHERE t.F_IsActive = 1 ");
            return new RepositoryFactory().BaseRepository().FindList<TSSchemeInfoEntity>(strSql.ToString());
        }


        public static Task<TSSchemeInfoEntity> GetSchemeInfoEntity(string keyValue)
        {
            return new RepositoryFactory().BaseRepository().FindEntityByKey<TSSchemeInfoEntity>(keyValue);
        }


        #endregion

        #region 提交数据

        /// <summary> 
        /// 删除实体数据
        /// </summary> 
        /// <param name="keyValue">主键</param> 
        /// <returns></returns> 
        public async Task DeleteEntity(string keyValue)
        {
            await new RepositoryFactory().BaseRepository().DeleteAny<TSSchemeInfoEntity>(new { F_Id = keyValue });
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// </summary> 
        /// <param name="keyValue">主键</param>
        /// <param name="entity">实体数据</param> 
        /// <returns></returns>
        public async Task SaveEntity(string keyValue, TSSchemeInfoEntity entity)
        {
            TSSchemeModel model = entity.F_Scheme.ToObject<TSSchemeModel>();
            if (model.startType == 1)
            {
                entity.F_BeginTime = DateTime.Now;
            }
            else
            {
                entity.F_BeginTime = model.startTime;
            }

            entity.F_EndType = model.endType;
            if (model.endType == 1)
            {
                entity.F_EndTime = DateTime.MaxValue.AddDays(-1);
            }
            else
            {
                entity.F_EndTime = model.endTime;
            }


            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.F_Id = keyValue;
                await new RepositoryFactory().BaseRepository().Update(entity);
            }
            else
            {
                entity.F_Id = Guid.NewGuid().ToString();
                entity.F_IsActive = 1;
                await new RepositoryFactory().BaseRepository().Insert(entity);
            }
        }
        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task UpdateEntity(string keyValue, TSSchemeInfoEntity entity)
        {
            entity.F_Id = keyValue;
            await new RepositoryFactory().BaseRepository().Update(entity);
        }


        //保存记录
        public static async Task SaveEntity(string keyValue, TSLogEntity entity)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.F_Id = keyValue;
                await new RepositoryFactory().BaseRepository().Update(entity);
            }
            else
            {
                entity.F_Id = Guid.NewGuid().ToString();
                entity.F_CreateDate = DateTime.Now;
                await new RepositoryFactory().BaseRepository().Insert(entity);
            }
        }




        #endregion

        #region 执行sql语句和存储过程
        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="code">数据库编码</param>
        /// <param name="sql">sql语句</param>
        /// <returns></returns>
        public static async Task ExecuteBySql(string code, string sql)
        {
            await new RepositoryFactory().BaseRepository(code).ExecuteSql(sql);
        }
        /// <summary>
        /// 执行存储过程
        /// </summary>
        /// <param name="code">数据库编码</param>
        /// <param name="name">名称</param>
        /// <returns></returns>
        public static async Task ExecuteProc(string code, string name)
        {
            await new RepositoryFactory().BaseRepository(code).ExecuteProc(name);
        }

        #endregion


    }
}
